package com.atwisdom.star.core.bsl.aop;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.business.Impl.DataChangeBslServiceImpl;
import com.atwisdom.star.core.bsl.business.utils.ConstantInfo;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dsl.modelClass.hier.Impl.HierInstanceDslServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Aspect
public class HierInstanceHandler {

    @Autowired
    private DataChangeBslServiceImpl dataChangeBslService;

    @Autowired
    private HierInstanceDslServiceImpl hierInstanceDslService;
    
    @Pointcut("@annotation(com.atwisdom.star.core.bsl.aop.HierInstanceAnnotation)")//用于匹配带有注解的方法
    private void pointcut(){
    }

    @Before("pointcut()")
    public void myBefore(JoinPoint joinpoint){
    }

    @After("pointcut()")
    public void myAfter(JoinPoint joinpoint) {
    }

    @Around("pointcut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        this.beforeDealSendData(methodName, args);
        Object proceed = joinPoint.proceed();
        afterDealSendData(methodName, proceed);
        return proceed;
    }

    private void afterDealSendData(String methodName, Object proceed) {
        if (methodName.equalsIgnoreCase("addChild")) {
            try {
                HierInstance hierInstance = (HierInstance)proceed;
                dealSendDataForAdd(hierInstance);
            } catch (Exception ex) {
                System.out.println("新增数据上传ERP失败");
                ex.printStackTrace();
            }
        }
    }

    private void beforeDealSendData(String methodName, Object[] args) {
        if(methodName.equalsIgnoreCase("updateById")){
            try {
                HierInstance hierInstance = (HierInstance) args[0];
                dealSendDataForUpdate(hierInstance);
            } catch (Exception ex) {
                System.out.println("更新数据上传ERP失败");
                ex.printStackTrace();
            }
        }else if(methodName.equalsIgnoreCase("deleteHierByIdList")){
            try {
                ActionData actionData = (ActionData<JSONObject>) args[0];
                dealSendDataForDelete(actionData);
            } catch (Exception ex) {
                System.out.println("删除数据上传ERP失败");
                ex.printStackTrace();
            }
        }
    }

    private void  dealSendDataForAdd( HierInstance hierInstance){
        String containerModel = hierInstance.getContainerModel();
        String className = hierInstance.getClassName();
        List<Long> idList = CollUtil.toList(hierInstance.getId());
        dataChangeBslService.apiSendDataBatchSave(idList, className, containerModel, ConstantInfo.OP_TYPE_ADD,true);
    }

    private void  dealSendDataForUpdate(HierInstance hierInstance){
        String containerModel = hierInstance.getContainerModel();
        String className = hierInstance.getClassName();
        List<Long> idList = CollUtil.toList(hierInstance.getId());
        dataChangeBslService.apiSendDataBatchSave(idList, className, containerModel, ConstantInfo.OP_TYPE_UPDATE,true);
    }

    private void  dealSendDataForDelete(ActionData actionData){
        Long id = actionData.getId();
        List<Long> idList = actionData.getIds();
        if(CollectionUtil.isEmpty(idList)){
            idList = new ArrayList<>();
            idList.add(id);
        }
        id=idList.get(0);
        HierInstance hierInstance = hierInstanceDslService.queryHierInsById(id);
        if(hierInstance == null){
            return;
        }
        String className = hierInstance.getClassName();
        String containerModel = hierInstance.getContainerModel();
        boolean need = dataChangeBslService.needTrans(className, containerModel, ConstantInfo.OP_TYPE_DELETE);
        if (need) {
            dataChangeBslService.apiSendDataBatchSave(idList, className, containerModel, ConstantInfo.OP_TYPE_DELETE,true);
        }
    }
}
